package com.wealth.commodity.web.controller;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;
import iaas.web.Servlets;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.cart.entity.Cart;
import com.wealth.cart.service.ICartService;
import com.wealth.commodity.entity.Category;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.entity.Search;
import com.wealth.commodity.enums.GoodsStatusEnum;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.service.ICommentService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.commodity.service.ISearchService;
import com.wealth.commodity.vo.CategoryVO;
import com.wealth.commodity.vo.GoodsVO;
import com.wealth.merchant.enums.MarketEnum;
import com.wealth.system.web.controller.BaseController;



/**
 * 商品(不区分商品是实体还是虚拟商品)
 * @author Administrator
 */
@Controller
@RequestMapping(value="/commodity")
public class GoodsController extends BaseController{
	
	Logger logger = Logger.getLogger(GoodsController.class);
	@Autowired 
	private ICategoryService categoryService;
	@Autowired 
	private IGoodsService goodsService;
	@Autowired 
	private ICommentService commentService;
	@Autowired
	private ICartService cartService;
	@Autowired
	private ISearchService iSearchService;
	
	
	/**
	 * 商品列表 跳转
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goodslist",method=RequestMethod.GET)
	public String searchHotWords(Model model,HttpServletRequest request,String name,Long categoryId) {
		try {
			model.addAttribute("name", name);
			//分类信息
			if(categoryId!=null){
				Category category=categoryService.get(categoryId);
				model.addAttribute("categoryId", categoryId);
				model.addAttribute("categoryName", category.getName());
			}
			//商品分类
			List<CategoryVO> categoryList = categoryService.getCategoryByStatusAndCorpId(StatusEnum.NORMAL.getValue(), 1L);
			model.addAttribute("categoryList", categoryList);
			/**查询pc端的热搜词*/
			List<Search> searchList = iSearchService.findByPCAndStatus();
			model.addAttribute("searchList", searchList);
			return "commodity/list";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	/**
	 * 首页搜索 跳转
	 * @param request
	 * @param memberid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/search",method=RequestMethod.POST)
	public String searchHotWords(Model model,HttpServletRequest request,@RequestParam String name) {
		try {
			
			model.addAttribute("name", name);
			return "commodity/list";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	
	
	/**
	 * 商品列表分页查询
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public ResultObject list(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "pageSize", defaultValue ="10") int pageSize,
			@RequestParam(value = "sortType", defaultValue = "createTime") String sortType,
			@RequestParam(value = "sortDirection", defaultValue = "desc") String sortDirection,
			Model model,ServletRequest request) {
		try {
			String name=request.getParameter("name");
			String categoryId=request.getParameter("categoryId");
			
			//查询
			Map<String,Object> map = goodsService.getGoods( pageNumber, pageSize, sortType, sortDirection, name, categoryId);
			@SuppressWarnings("unchecked")
			List<Goods> list = (List<Goods>) map.get("list");
			String count = "0";
			if(map.get("count")!=null){
				 count = map.get("count").toString();
			}
			
			//商品列表
			return new ResultObject("000",count, list); 
		} catch (Exception e) {
			logger.error("商品列表错误", e);
			return null;
		}
	}
	
	
	/**
	 * 商品详情(惠生活和品牌购两种)
	 * @param request
	 * @param goodsid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/info")
	public String info(HttpServletRequest request, Model model,Long goodsId) {
		
		try {
			//购物车
			if(SecurityUtils.getSubject().isAuthenticated()){
				
				int count = 0;
				Cart cart=cartService.findCartByMemberIdAndMarketId(memberid, 3L);
				if(cart!=null){
					for (int i = 0; i < cart.getCartItems().size(); i++) {
						count = count + cart.getCartItems().get(i).getAmount();
					}
				}
				model.addAttribute("cart",cart);
				if(cart.getCartItems()!=null){
				  model.addAttribute("cartCount", count);
				}
			}
			GoodsVO goodsVO=goodsService.getGoods(goodsId);
			model.addAttribute("goodsVO", goodsVO);
			
			Long markeId = goodsVO.getGoods().getMerchant().getMarket().getId();
			boolean isLife=false;
			//惠生活
			if(markeId==MarketEnum.LIFE.getValue()){
				isLife=true;
				//商户ID + 状态   获取该商户的其他商品
				List<Goods> goodsList=goodsService.getGoodsByMerchantIdAndStatus(goodsVO.getGoods().getMerchant().getId(), GoodsStatusEnum.UP.getValue());
				for (int i = 0; i < goodsList.size(); i++) {
					if(goodsList.get(i).getId()==goodsId){
						goodsList.remove(i);
					}
				}
				model.addAttribute("goodsList", goodsList);
			}  
			model.addAttribute("isLife", isLife);
			//商品分类
			List<CategoryVO> categoryList = categoryService.getCategoryByStatusAndCorpId(StatusEnum.NORMAL.getValue(), 1L);
			model.addAttribute("categoryList", categoryList);
			/**查询pc端的热搜词*/
			List<Search> searchList = iSearchService.findByPCAndStatus();
			model.addAttribute("searchList", searchList);
			return "commodity/goodsDetail";
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	
	
	/**
	 * 商品评价页面
	 * @param request
	 * @param goodsid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/comment")
	public String comment(HttpServletRequest request, Model model,Long goodsId,Long marketId) {
		
		try {
			
			GoodsVO goodsVO=goodsService.getGoods(goodsId);
			model.addAttribute("goodsVO", goodsVO);

			model.addAttribute("marketId", marketId);
			
			return "commodity/comment";
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	/**
	 * 商品评价列表查询
	 */
	@RequestMapping(value = "/commentlist")
	@ResponseBody
	public ResultObject commentlist(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "pageSize", defaultValue ="10") int pageSize,
			@RequestParam(value = "sortType", defaultValue = "createTime") String sortType,
			@RequestParam(value = "sortDirection", defaultValue = "desc") String sortDirection,
			Model model,ServletRequest request) {
		try {
			String goodsId=request.getParameter("goodsId");
			//查询
			Map<String,Object> map = commentService.getComment(pageNumber, pageSize, sortType, sortDirection, Long.valueOf(goodsId));
			@SuppressWarnings("unchecked")
			List<Goods> list = (List<Goods>) map.get("list");
			String count = "0";
			if(map.get("count")!=null){
				 count = map.get("count").toString();
			}
			
			//商品列表
			return new ResultObject("000",count, list); 
		} catch (Exception e) {
			logger.error("商品列表错误", e);
			return null;
		}
	}
	
	
	
	
}
